<?php
require "../../system/framework.php";
//require_once("../../system/api/simplehtmldom/simple_html_dom.php");


$title=request::generalRequest('title');
$template=request::generalRequest('template');

// falls template als id übergeben wurde
if (isset($_REQUEST['__template'])) {
    $templateId = $_REQUEST['__template'];
    $template = db::getValue("SELECT template FROM _template WHERE id='{$templateId}'");
}





$usergroup=request::generalRequest('usergroup');
// falls usergroup als id übergeben wurde
if (isset($_REQUEST['_usergroup'])) {
    $usergroupId = $_REQUEST['_usergroup'];
    $usergroup = db::getValue("SELECT usergroup_number FROM usergroup WHERE id='{$usergroupId}'");
}





$content = new content();


$menuActive = 0;
if (isset($_REQUEST['menu_active'])) {
    $menuActive = 1;
}
     
     //$menuActive = false;
if (isset($_REQUEST['menu_active'])) {
    //$menuActive = true;
    $content->menuActive = 1;  
}


// Url
if (isset($_REQUEST['url'])) {
    //$menuActive = true;
    $content->url = $_REQUEST['url'];
}

                 
 
// Text
if (isset($_REQUEST['text'])) {
    //$menuActive = true;
    $content->text = $_REQUEST['text'];
}
                 
                 
      
// falls Content Update
if (isset($_REQUEST['content_id'])) {
   $content->contentId = $_REQUEST['content_id'];
}      
      
      
      
                    

$content->title = $title;
$content->template = $template;
//$content->parentUrl = "/web";

// Parent Url
if (isset($_REQUEST['parent_url'])) {
    $content->parentUrl = $_REQUEST['parent_url'];
}


// Redirect
if (isset($_REQUEST['redirect'])) {
    $content->redirect = $_REQUEST['redirect'];
}





$content->menuActive = $menuActive;
$content->usergroup = $usergroup;



//$content->text = '<p>hello world</p>';
//$content->url = "/hello-9";


//$content->createContentFile = true;
//$content->template = "admin.html";
$url = $content->createContent();



// save user_group

            //$nTable = $this->table;
            //$nmTable=$nTable . "_" . $mTable;

            
            $contentId = db::getId("content", "_url='{$url}'");

            //db::sqlExec("DELETE FROM {$this->nmTable} WHERE _" . $this->nTable . "='$id'");
            db::sqlExec("DELETE FROM user_group WHERE _user='$contentId'");


            foreach (db::getData("group") as $data)
                {

                $field = "nm_user_group_" . $data['id'];

                if (isset($_REQUEST[$field]))
                    {
                    $dataNm=array();
                    $dataNm['_user']=$contentId;
                    $dataNm['_group']=$data['id'];

                    db::saveData("user_group", $dataNm);
                    }
                }










//$usergroupNumber = 0;

//$template




//$filename = convert::convertFilename($title).".html";
/*$filename=convert::convertToMachineCode($title) . ".html";

$contentPath=config::$projectPath . "/content";
$filename=$contentPath . "/" . $filename;

file::createHtmlFile($filename, $title, "", false);


// Content erstellen
               /*     $content=new content();
                    $content->title=$title;
                    $content->url=$url;
                 //   $content->redirect = $contentFile->redirect;
                    $content->text=$text;
                    $content->contentPath = $contentPath;
                    $content->contentFilename=$filenameFrom;
                    $content->template=$template;
                    $content->controller=$controller;
                    $content->header=$header;
                    $content->parentUrl=$this->parentUrl;
                    $content->usergroup = $usergroup;
                    $content->usergroupEdit = $usergroupEdit;
                    $content->compile=true;
                    $content->menuActive=$menuActive;
                    $content->titleActive=$titleActive;
                    $content->createPage();*/



/*$compileContentFile=new compile_content_file();
$compileContentFile->baseDir=false;
$compileContentFile->contentPath=$contentPath;
$compileContentFile->codePath=config::$basePath . "/code";
$compileContentFile->parentUrl="/web";
$compileContentFile->overwriteOrginalFile=true; 

 

$url=$compileContentFile->compileContentFile($filename);  */   


// einf?gen in menu.xml
/*$menuFilename=config::$projectPath . "/menu/menu.xml";
//$notListedFilename=config::$projectPath . "/menu/not_listed.xml";

$xml=simplexml_load_file($menuFilename);

/*$xmlNotListed=new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><config></config>');
*/

//   $title = "Programm";
//$title=$content['title'];
//$url=$content['_url'];

/*$a=$xml->xpath('//content[@url="' . $url . '"]');
//$a = $xml->xpath('content[@url="/produkt2"]');
//$a = $xml->xpath('//content[@url"/produkt2"]');


//        $nodes = $this->xml->xpath('/config/table[@name="'.$table.'"]');

//$a = $xml->xpath('content');

if (empty($a))
    {
    $tableChild=$xml->content[0]->addChild("content", "");
    $tableChild->addAttribute("title", $title);
    $tableChild->addAttribute("url", $url);
    }


// Xml mit Not Listed Men?'s'
file::createTextFile($menuFilename, $xml->saveXML(), false);  */


// redirect
http::redirect($url);
?>
